package com.woniu.shop.controller;


import com.woniu.constant.Constant;
import com.woniu.entity.PageResult;
import com.woniu.entity.Result;
import com.woniu.entity.ResultCode;
import com.woniu.shop.entity.dto.QueryServiceDTO;
import com.woniu.shop.entity.dto.StoreServiceDTO;
import com.woniu.shop.pojo.StoreService;
import com.woniu.shop.service.IStoreServiceService;
import com.woniu.shop.utils.AliyunUploadUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 门店服务表 前端控制器
 * </p>
 *
 * @author LiFangyu
 * @since 2023-02-15
 */
@Api(tags = "服务申请控制器")
@RestController
@RequestMapping("/store-service")
public class StoreServiceController {

    @Autowired
    private IStoreServiceService storeServiceService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("添加服务")
    @PostMapping("/add/{status}")
    public Result addService(@RequestBody StoreServiceDTO storeServiceDTO, @PathVariable String status) {
        return storeServiceService.addService(storeServiceDTO, status);
    }

    @ApiOperation("更改状态")
    @PutMapping("/{id}/{status}")
    public Result updateStatus(@PathVariable String id, @PathVariable String status) {
        return storeServiceService.updateStatus(id, status);
    }

    @ApiOperation("图片上传")
    @PostMapping("/img")
    public Result upload(@RequestPart MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        String uuid = UUID.randomUUID().toString();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        String fileName = uuid.replaceAll("-", "") + substring;
        AliyunUploadUtils.uploadOneFile(file, fileName);
        stringRedisTemplate.opsForSet().add(Constant.REDIS_ALL_UPLOAD_IMAGE, fileName);
        return Result.ok(fileName);
    }

    @ApiOperation("用户根据类型查询服务")
    @GetMapping("/type/{id}")
    public Result<List<StoreService>> queryByType(@PathVariable String id) {
        List<StoreService> listAll = storeServiceService.queryByType(id);
        if (listAll != null && listAll.size() > 0) {
            return Result.ok(listAll);
        }
        return Result.error(ResultCode.RESULE_DATA_NONE.getCode(), ResultCode.RESULE_DATA_NONE.getMessage());
    }

    @ApiOperation("门店查询服务")
    @PostMapping ("/query/{currentPage}/{currentSize}")
    public Result<PageResult<StoreService>> queryServiceByShop(@PathVariable Integer currentPage,@PathVariable Integer currentSize ,@RequestBody QueryServiceDTO queryServiceDTO){

        PageResult<StoreService> pageResult = storeServiceService.queryServiceByShop(currentPage,currentSize,queryServiceDTO);
        if (pageResult.getTotal() > 0){
            return Result.ok(pageResult);
        }
        return Result.error(ResultCode.RESULE_DATA_NONE.getCode(), ResultCode.RESULE_DATA_NONE.getMessage());
    }

    @ApiOperation("根据id删除服务")
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable String id){
        boolean flag = storeServiceService.removeById(id);
        if (flag) {
            return Result.ok();
        }
        return Result.error(ResultCode.ERROR.getCode(),ResultCode.ERROR.getMessage());
    }

    @ApiOperation("根据服务id获取服务")
    @GetMapping("/getService/{id}")
    public StoreService getService(@PathVariable("id") String id){
        return storeServiceService.getService(id);
    }
}
